const app = getApp()
var util = require('../../utils/util.js')
var http = require('../../utils/http.js')


Page({
  data: {
    photo: []
  },
  onLoad: function (options) {
    var that = this
    that.setData({
      navH: app.globalData.navHeight
    })
    that.setData({
      title: options.title
    })
  },
  // 上传图片
  uploadphoto: function () {
    var that = this;
    var resurl = app.globalData.resurl;
    var photo = that.data.photo;
    var count = 4 - photo.length;
    var counts = photo.length;
    wx.chooseImage({
      count: count, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        wx.showLoading({
          title: "正在上传图片",
        })
        for (var i = 0; i < res.tempFilePaths.length; i++) {
          var file = res.tempFilePaths[i];
          console.log(file)
          var index = counts + i;
          that.uploadFile(file, index);
        }
      }
    })
  },
  // 图片上传 
  uploadFile: function (file, index) {
    var that = this;
    console.log(file)
    var photo = that.data.photo;
    console.log(photo)
    var resurl = app.globalData.resurl;
    wx.uploadFile({
      url: resurl + "/cosUpload/upload.shtml",
      formData: {
        filePath: "pig/suggest/"
      },
      filePath: file,
      name: "file",
      success: function (data) {
        console.log(data);
        photo[index] = data.data;
        that.setData({
          photo: photo
        })
        console.log(photo);
        console.log("上传照片成功后")
        wx.hideLoading();
      }
    });
  },
  // 输入框
  input: function (e) {
    var that = this;
    var val = e.detail.value;
    that.setData({
      val: val
    })
  },
  btn:function() {
    wx.showModal({
      title: '反馈成功',
      content: '您的投诉与建议已经提交，我们会认真核实处理',
      showCancel: false,
      success(res) {
        if (res.confirm) {
          wx.navigateBack({
            delta: 3
          })
        }
      }
    })
  },
  // 提交
  // btn: function () {
  //   var that = this;
  //   var resurl = app.globalData.resurl;
  //   var key = app.globalData.key;
  //   var deviceProperties = app.globalData.deviceProperties;
  //   var photo = that.data.photo;
  //   var title = that.data.title;
  //   var val = that.data.val;
  //   console.log(val)
  //   if (photo.length == 0) {
  //     if (val == undefined || val == "") {
  //       wx.showToast({
  //         title: '请填写投诉描述',
  //         icon: 'none',
  //         duration: 2000
  //       })
  //       return false;
  //     }
  //   }
  //   photo = photo.join("-");
  //   console.log(photo);
  //   var data15 = {
  //     deviceProperties: deviceProperties,
  //     complaint: {
  //       content: val,
  //       complaintType: title,
  //       complaintImg: photo
  //     }
  //   }
  //   console.log(data15)
  //   data15 = JSON.stringify(data15);
  //   data15 = util.base64_encode(data15);
  //   data15 = util.xxtea_encrypt(data15, key);
  //   wx.request({
  //     url: resurl + '/interface/sdkData.shtml?requestId=14&a=1&b=0',
  //     data: data15,
  //     header: {
  //       'Content-Type': 'application/json;charset=utf-8'
  //     },
  //     method: "POST",
  //     success: function (res) {
  //       res = util.xxtea_decrypt(res.data, key);
  //       res = util.base64_decode(res);
  //       res = JSON.parse(res);
  //       console.log(res);
  //       wx.showModal({
  //         title: '反馈成功',
  //         content: '您的投诉与建议已经提交，我们会认真核实处理',
  //         showCancel: false,
  //         success(res) {
  //           if (res.confirm) {
  //             wx.navigateBack({
  //               delta: 3
  //             })
  //           }
  //         }
  //       })

  //     }
  //   })
  // },
   // 返回上一页
  navBack: function () {
    wx.navigateBack({
      delta: 1
    })
  },
})